﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Snowdreamist.Collection;

namespace CompositionPlugin.Service
{
    public class ModuleMonitor : IModuleMonitor
    {
        public ModuleMonitor()
        {
            this.Modules = new ReadonlyConcurrentObservableCollection<SystemRuntime.Module>(this._modules);
        }

        public virtual void Initialize() { }

        public ReadonlyConcurrentObservableCollection<SystemRuntime.Module> Modules { get; private set; }

        public void OnModuleActiving<T>(SystemRuntime.Module<T> module) where T : class
        {
            lock (this)
            {
                this._modules.Add(module);
            }
        }

        public void OnModuleDestroying<T>(SystemRuntime.Module<T> module) where T : class
        {
            lock (this)
            {
                this._modules.Remove(module);
            }
        }

        public virtual void OnRuntimeDisposing()
        {
            this._modules.Dispose();
        }

        #region private

        private readonly ConcurrentObservableCollection<SystemRuntime.Module> _modules = new ConcurrentObservableCollection<SystemRuntime.Module>();

        #endregion private
    }
}